package com.example.demo.core.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author yj
 * @date 2021/4/7 14:06
 */
@Slf4j
public class RegexUtil {


    /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     * <pre>
     * RegexUtil.checkObjectIsNull(null)      = true
     * RegexUtil.checkObjectIsNull("")        = true
     * RegexUtil.checkObjectIsNull(" ")       = true
     * RegexUtil.checkObjectIsNull("bob")     = false
     * RegexUtil.checkObjectIsNull("  bob  ") = false
     * RegexUtil.checkObjectIsNull(1)         = false
     * RegexUtil.checkObjectIsNull(false)     = false
     * </pre>
     */
    public static boolean checkObjectIsNull(Object data) {
        if (data instanceof String) {
            return checkStringIsNull((String) data);
        } else if (data instanceof Map) {
            Map<Object, Object> map = JsonUtil.jsonObjectToType(data, new TypeReference<Map<Object, Object>>() {
            });
            return map == null || map.isEmpty();
        } else if (data instanceof List) {
            List<Object> list = JsonUtil.jsonObjectToType(data, new TypeReference<List<Object>>() {
            });
            return list == null || list.isEmpty();
        } else if (data instanceof Set) {
            Set<Object> set = JsonUtil.jsonObjectToType(data, new TypeReference<Set<Object>>() {
            });
            return set == null || set.isEmpty();
        }
        return data == null;
    }

    /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     * <pre>
     * RegexUtil.checkStringIsNull(null)      = true
     * RegexUtil.checkStringIsNull("")        = true
     * RegexUtil.checkStringIsNull(" ")       = true
     * RegexUtil.checkStringIsNull("bob")     = false
     * RegexUtil.checkStringIsNull("  bob  ") = false
     * </pre>
     */
    public static boolean checkStringIsNull(String data) {
        return StringUtils.isBlank(data);
    }

    /**
     * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
     * <pre>
     * RegexUtil.checkObjectIsNotNull(null)      = false
     * RegexUtil.checkObjectIsNotNull("")        = false
     * RegexUtil.checkObjectIsNotNull(" ")       = false
     * RegexUtil.checkObjectIsNotNull(1)         = true
     * RegexUtil.checkObjectIsNotNull(false)     = true
     * </pre>
     */
    public static boolean checkObjectIsNotNull(Object data) {
        if (data instanceof String) {
            return checkStringIsNotNull((String) data);
        } else if (data instanceof Map) {
            Map<Object, Object> map = JsonUtil.jsonObjectToType(data, new TypeReference<Map<Object, Object>>() {
            });
            return map != null && map.size() != 0;
        } else if (data instanceof List) {
            List<Object> list = JsonUtil.jsonObjectToType(data, new TypeReference<List<Object>>() {
            });
            return list != null && list.size() != 0;
        } else if (data instanceof Set) {
            Set<Object> set = JsonUtil.jsonObjectToType(data, new TypeReference<Set<Object>>() {
            });
            return set != null && set.size() != 0;
        }
        return data != null;
    }

    /**
     * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
     * <pre>
     * RegexUtil.checkStringIsNotNull(null)      = false
     * RegexUtil.checkStringIsNotNull("")        = false
     * RegexUtil.checkStringIsNotNull(" ")       = false
     * RegexUtil.checkStringIsNotNull("bob")     = true
     * RegexUtil.checkStringIsNotNull("  bob  ") = true
     * </pre>
     */
    public static boolean checkStringIsNotNull(String data) {
        return !StringUtils.isBlank(data);
    }

}